package com.kgc.storeprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.InventoryLoss;
import com.kgc.store.pojo.InventoryProfit;
import com.kgc.storeprovider.dao.InventoryLossDetailMapper;
import com.kgc.storeprovider.dao.InventoryLossMapper;
import com.kgc.storeprovider.dao.InventoryProfitDetailMapper;
import com.kgc.storeprovider.dao.InventoryProfitMapper;
import com.kgc.storeprovider.service.InventoryProfitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class InventoryProfitServiceImpl implements InventoryProfitService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private InventoryProfitMapper inventoryProfitMapper;

    @Autowired
    private InventoryProfitDetailMapper inventoryProfitDetailMapper;

    @Override
    public ResponseMsg getInventoryProfitList(Map<String, Object> map) {
        logger.info("InventoryProfitServiceImpl getInventoryProfitList map:"+map);
        ResponseMsg msg = new ResponseMsg();
        PageInfo<InventoryProfit> pageInfo = new PageInfo<>();
        if(map.containsKey("pageNo")){
            pageInfo.setPageNo(Integer.valueOf(map.get("pageNo").toString()));
        }
        if(map.containsKey("pageSize")){
            pageInfo.setPageSize(Integer.valueOf(map.get("pageSize").toString()));
        }
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<InventoryProfit> inventoryProfitList = inventoryProfitMapper.getInventoryProfitList(map);
        logger.debug("InventoryProfitServiceImpl getInventoryProfitList inventoryProfitList:"+inventoryProfitList);
        pageInfo.setList(inventoryProfitList);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal((int)page.getTotal());
        msg.setCode("200");
        msg.setData(pageInfo);
        logger.debug("InventoryProfitServiceImpl getInventoryProfitList msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg delInventoryProfit(String code) {
        ResponseMsg msg = new ResponseMsg();
        int delCount = inventoryProfitMapper.delInventoryProfit(code);
        inventoryProfitDetailMapper.delInventoryProfitDetail(code);
        if(delCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg insertInventoryProfit(InventoryProfit inventoryProfit) {
        ResponseMsg msg = new ResponseMsg();
        int insertCount = inventoryProfitMapper.insertInventoryProfit(inventoryProfit);
        if(insertCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }
}
